{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "    def findNthDigit(self, n):\n",
    "        \"\"\"\n",
    "        :type n: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if n < 10:\n",
    "            return n\n",
    "        \n",
    "        base = 1\n",
    "        x = 1\n",
    "        while True:\n",
    "            block_sum = 9*base*x\n",
    "            if n < block_sum:\n",
    "                break\n",
    "            n = n - block_sum\n",
    "            base = base * 10\n",
    "            x = x + 1\n",
    "        \n",
    "        index = n // x + base\n",
    "        rest = n % x\n",
    "        if rest == 0:\n",
    "            return int(str(index-1)[-1])\n",
    "        else:\n",
    "            return int(str(index)[rest-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = Solution()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "0\n",
      "0\n",
      "0\n",
      "1\n",
      "0\n",
      "0\n",
      "1\n",
      "1\n",
      "0\n",
      "0\n",
      "2\n",
      "1\n",
      "0\n",
      "0\n",
      "3\n",
      "1\n",
      "0\n",
      "0\n",
      "4\n",
      "1\n",
      "0\n",
      "0\n",
      "5\n",
      "1\n",
      "0\n",
      "0\n",
      "6\n",
      "1\n",
      "0\n",
      "0\n",
      "7\n",
      "1\n",
      "0\n",
      "0\n",
      "8\n",
      "1\n",
      "0\n",
      "0\n",
      "9\n",
      "1\n",
      "0\n",
      "1\n",
      "0\n",
      "1\n",
      "0\n",
      "1\n",
      "1\n",
      "1\n",
      "0\n",
      "1\n",
      "2\n",
      "1\n",
      "0\n",
      "1\n",
      "3\n",
      "1\n",
      "0\n",
      "1\n",
      "4\n",
      "1\n",
      "0\n",
      "1\n",
      "5\n",
      "1\n",
      "0\n",
      "1\n",
      "6\n",
      "1\n",
      "0\n",
      "1\n",
      "7\n",
      "1\n",
      "0\n",
      "1\n",
      "8\n",
      "1\n",
      "0\n",
      "1\n",
      "9\n",
      "1\n",
      "0\n",
      "2\n",
      "0\n",
      "1\n",
      "0\n",
      "2\n",
      "1\n",
      "1\n",
      "0\n",
      "2\n",
      "2\n",
      "1\n",
      "0\n",
      "2\n",
      "3\n",
      "1\n",
      "0\n",
      "2\n",
      "4\n",
      "1\n",
      "0\n",
      "2\n",
      "5\n",
      "1\n",
      "0\n",
      "2\n",
      "6\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "for i in range(2890,3000):\n",
    "    print(s.findNthDigit(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
